# Definition for singly-linked list.
class ListNode:
  def __init__(self, x):
    self.val = x
    self.next = None


class Solution:
  def reverseList(self, head: ListNode) -> ListNode:
    vals = []
    while head is not None:
      vals.append(head.val)
      head = head.next
    if len(vals) == 0:
      return None
    new_head = ListNode(vals[-1])
    new_head_sav = new_head
    for i in range(len(vals)-2, -1, -1):
      new_head.next = ListNode(vals[i])
      new_head = new_head.next
    return new_head_sav


'''
STATUS: SOLVED
TRIES:  1
TIME:   68 MS
MEMORY: 15.4 MB
'''
if __name__ == '__main__':
  solution = Solution()
  nodes = [1, 2, 3, 4, 5]
  head = ListNode(1)
  head_sav = head
  for i in range(1, len(nodes)):
    head.next = ListNode(nodes[i])
    head = head.next
  res = solution.reverseList(head_sav)
  while res is not None:
    print(res.val, end=', ')
    res = res.next
